package com.udaos.modules.sales.controllers;

import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.udaos.common.filter.PrivateFilter;
import com.udaos.common.page.DataTableColumn;
import com.udaos.common.page.DataTableOrder;
import com.udaos.models.sys.Sys_user;
import com.udaos.modules.customer.services.UdCustomerService;
/**
 * 推广人员
 * @author mutaotao
 *
 */
import com.udaos.modules.sales.services.UdSalesService;
import com.udaos.modules.sales.services.UdSalesTxService;
import com.udaos.modules.wx.services.UdWxQrsceneQrcodeService;
@IocBean
@At("/platform/sales")
@Filters({ @By(type = PrivateFilter.class)})
public class UdSalesController {
	
	@Inject
	private UdSalesService salesService; 
	@Inject
	private UdCustomerService customerService;
	@Inject
	private UdSalesTxService salesTxService;
	@Inject
	private UdWxQrsceneQrcodeService qrsceneQrcodeService;
	
	
	/**
	 * 首页
	 */
	@At("")
	@Ok("beetl:/platform/sales/index.html")
	@RequiresAuthentication
	public void index() {
		
	}
	
	/**
	 * 加载数据
	 * @param name
	 * @param length
	 * @param start
	 * @param draw
	 * @param order
	 * @param columns
	 * @return
	 */
	@At
	@Ok("json:full")
	@RequiresAuthentication
	public Object data(@Param("length") int length, @Param("start") int start, @Param("draw") int draw, @Param("::order") List<DataTableOrder> order, @Param("::columns") List<DataTableColumn> columns) {
		Cnd cnd = Cnd.NEW();
		cnd.and("state", "=", 1);
		Subject subject = SecurityUtils.getSubject();
        Sys_user user = (Sys_user) subject.getPrincipal();
        String companyId=user.getUnit().getCompany_id();
        cnd.and("company_id", "=", companyId);
		return salesService.data(length, start, draw, order, columns, cnd, null);
	}
	
	/**
	 * 获取客户
	 * @param id
	 * @return
	 */
	@At
	@Ok("json")
	public Object getCustomer(String id){
		return customerService.fetch(id);
	}
	
	/**
	 * 提现申请列表
	 */
	@At("/txIndex")
	@Ok("beetl:/platform/sales/tx_index.html")
	@RequiresAuthentication
	public void txIndex() {
		
	}
	/**
	 * 加载数据
	 * @param name
	 * @param length
	 * @param start
	 * @param draw
	 * @param order
	 * @param columns
	 * @return
	 */
	@At("/txData")
	@Ok("json:full")
	@RequiresAuthentication
	public Object txData(@Param("length") int length, @Param("start") int start, @Param("draw") int draw, @Param("::order") List<DataTableOrder> order, @Param("::columns") List<DataTableColumn> columns) {
		Cnd cnd = Cnd.NEW();
		cnd.and("state", "=", 0);
		Subject subject = SecurityUtils.getSubject();
        Sys_user user = (Sys_user) subject.getPrincipal();
        String companyId=user.getUnit().getCompany_id();
        cnd.and("company_id", "=", companyId);
		return salesTxService.data(length, start, draw, order, columns, cnd, null);
	}
	
	/**
	 * 发放申请
	 * @param id
	 * @return
	 */
	@At("/fafang")
	@Ok("json")
	public Object fafang(String id){
		Map<String,Object> map=salesTxService.fafang(id);
		return map;
		
	}
	
	
	/**
	 * 首页
	 */
	@At("/qrcodeIndex")
	@Ok("beetl:/platform/sales/qrcode_index.html")
	@RequiresAuthentication
	public void QrcodeIndex() {
		
	}
	/**
	 * 加载数据
	 * @param length
	 * @param start
	 * @param draw
	 * @param order
	 * @param columns
	 * @return
	 */
	@At("/QrcodeData")
	@Ok("json:full")
	@RequiresAuthentication
	public Object QrcodeData(@Param("length") int length, @Param("start") int start, @Param("draw") int draw, @Param("::order") List<DataTableOrder> order, @Param("::columns") List<DataTableColumn> columns) {
		return qrsceneQrcodeService.data(length, start, draw, order, columns, null, null);
	}
	
	/**
	 * 获取客户
	 * @param id
	 * @return
	 */
	@At
	@Ok("json")
	public Object getQrcodeNum(String qrscene){
		return qrsceneQrcodeService.getQrcodeNum(qrscene);
	}
	

}
